<template>
  <div class="main">
    <ul class="commentList" ref="list" v-if="true">
      <li class="comment" 
        v-for="item in 20" 
        :class="{'active': active==item}"
        ref="item" 
        :id="'id'+item" 
      >
        <div class="img">
          <img src="../assets/logo.png">
        </div>
        <p class="time">2019-01-01 id: {{item}}</p>
        <p class="desc">这只是详情内容而已啊啊啊啊啊啊</p>
      </li>
    </ul>
    <menu>
      <ul>
        <li v-for="item in 20" :id="'menuid'+item">
          <p @click="jump(item)">这是 item {{item}}</p>
        </li>
        <i class="active" :style="{'top': menuTop + 'px'}"></i>
      </ul>
    </menu>
  </div>
</template>

<script>
import Vue from 'vue'

export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
      loading: true,
      load: {
        loading: true,
        length: 5
      },
      menuTop: 0,
      active: 0,
      show: false
    }
  },
  created () {
    setTimeout(() => {
      this.loading = false
      this.load.loading = false
      console.log('加载数据');
    }, 5000)
  },
  mounted () {
    this.show = true
    // this.jump(1)
    window.addEventListener('scroll', this.jump)
  },
  methods: {
    handleScroll () {
      const top = document.documentElement.scrollTop;
      // console.log(top, 'top');
    },
    jump () {
      // console.log(this.$refs.list);
      let scrollTop = document.documentElement.scrollTop
      let liEl = this.$refs.item
      // console.log(liEl);
      let list = []
      let salidId = 1
      liEl.forEach(item => {
        console.log(item.offsetTop);
        if (scrollTop > item.offsetTop - 40) {
          salidId = item.id
        }
        console.log(salidId, 'salidId');
        // list.push(item.id)
      })
      // console.log(list);
      // 这种方式只能跳转到固定id，不能加减数值
      // document.getElementById('id'+id).scrollIntoView({
      //   block: 'start',
      //   behavior: 'smooth'
      // })
      // this.active = id
      this.menuTop = this.$el.querySelector("#menu"+salidId).offsetTop + 10
      // let time = setTimeout(() => {
      //   // li[0].classList.remove('active')
      //   this.active = ''
      // }, 3000)
      // document.getElementById('menu'+)
      // let top = this.$el.querySelector("#menu"+salidId).offsetTop - 50
      // window.scrollTo({
      //   top: top,
      //   behavior: "smooth"
      // });
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.main {
  display: grid;
  grid-template-columns: 5fr 2fr;
  gap: 20px;
  font-size: 14px;
}
menu{
  position: relative;
  background: #f8fbfb;
  padding: 0 12px;
  overflow-y: auto;
}
menu ul {
  position: fixed;
}
menu ul li{
  line-height: 40px;
  height: 40px;
}
menu ul i{
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #42b983;
  display: block;
  top: 20px;
  position: absolute;
  transition: all 0.2s linear 0s;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
.commentList{
  color: #333;
  /* position: absolute; */
  top: 10px;
  position: relative;
}
.comment{
  display: grid;
  grid-template-columns: 2fr 4fr;
  grid-template-rows: 1fr 3fr;
  grid-template-areas: 
    "img time"
    "img desc";
  gap: 20px;
}
.comment.active{
  animation: bgColor 1.5s linear ;
}
.comment .img{
  grid-area: img;
  width: 100px;
}
.comment .time{
  grid-area: time;
}
.comment .desc{
  grid-area: desc;
}
img{
  width: 100%;
}

@keyframes bgColor{
  50%{background: #47c0f7;}
  100%{background: transparent;}
}
</style>
